package com.kay.web;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * 实现了ApplicationContextAware的Bean对象，
 * 在Spring初始化bean信息（调用 initializeBean方法）时，会调用其 setApplicationContext方法
 */
@Component
public class MyApplicationContext implements ApplicationContextAware {
    private static ApplicationContext context;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    //从IOC容器中获取DispatcherServlet
    public static DispatcherServlet getDispatcherServlet() {
        return context.getBean(DispatcherServlet.class);
    }
}
